package com.njust.controller;

import com.njust.Util.ExcelUtil;
import com.njust.domain.dto.*;
import com.njust.domain.vo.TrainPageQueryPersonVO;
import com.njust.limit.Limit;
import com.njust.result.Result;
import com.njust.service.TrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/7 16:53
 * @培训考核相关接口(人)
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/train")
@Slf4j
public class TrainController {
    @Autowired
    private TrainService trainService;
    @Autowired
    private ExcelUtil excelUtil;

    /**
     * 申请培训
     * @param trainDTO
     * @return
     */
    @PostMapping("/joinInTrain")
    public Result<String> joinInTrain(@RequestBody TrainDTO trainDTO){
        log.info("人员申请参加培训，{}",trainDTO);
        trainService.joinInTrain(trainDTO);
        return Result.success("申请成功");
    }

    /**
     * 上传试卷
     *
     * @param trainTestPaperDTO
     * @return
     */
    @PostMapping("/upload_pdf")
    public Result<String> uploadPdf(@RequestBody TrainTestPaperDTO trainTestPaperDTO) {
        log.info("上传试卷，参数{}",trainTestPaperDTO);
        trainService.upload_pdf(trainTestPaperDTO);
        return Result.success("上传试卷成功");
    }

    /**
     * 下载试卷
     *
     * @param project_id
     * @return
     * @throws FileNotFoundException
     */
    @GetMapping("/download_pdf/{project_id}")
    public Result<String> download(@PathVariable int project_id) throws FileNotFoundException {
        log.info("下载试卷 项目id,{}",project_id);
        return Result.success(trainService.download(project_id),"下载成功");
    }


    /**
     * 下载证书
     * @param trainDownloadCertDTO
     * @return
     */
    @GetMapping("/download_cert")
    public Result<String> download_cert(TrainDownloadCertDTO trainDownloadCertDTO) {
        log.info("下载证书,{}",trainDownloadCertDTO);
        String url = trainService.download_cert(trainDownloadCertDTO);
        return Result.success(url, "下载证书成功");
    }

    /**
     * 分页查询人员信息
     * @param trainPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery_person")
    @Limit(key = "limitPageQueryPerson", permitsPerSecond = 1, timeout = 500, timeunit = TimeUnit.MILLISECONDS,msg = "请求繁忙，请稍后再试！")
    public Result<TrainPageQueryPersonVO> pageQueryPerson(TrainPageQueryDTO trainPageQueryDTO) {
        log.info("查询人员授权清单,{}",trainPageQueryDTO);
        TrainPageQueryPersonVO trainPageQueryPersonVO= trainService.queryPerson(trainPageQueryDTO);
        return Result.success(trainPageQueryPersonVO, "分页查询人员培训状态成功");
    }

    /**
     * 修改人员状态并且生成证书
     * @param assessment_status
     * @param trainPersonDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/status/{assessment_status}")
    public Result<String> updateStatus(@PathVariable int assessment_status, @RequestBody TrainPersonDTO trainPersonDTO) throws Exception {
        log.info("更新状态为{}，{}",assessment_status,trainPersonDTO);
        trainService.updateStatus(assessment_status,trainPersonDTO);
        return Result.success("更新状态成功");
    }

    /**
     * 导出人员授权清单
     * @param response
     * @param project_id
     */
    @GetMapping("/export_person/{project_id}")
    public Result<String> export_person(HttpServletResponse response, @PathVariable int project_id) {
        log.info("导出人员授权清单 项目id,{}",project_id);

        excelUtil.exportPersonExcel(response,project_id);
       return Result.success("导出人员授权清单成功");
    }

    /**
     * 根据项目id分页查询项目授权清单
     * @param trainPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery_project")
    public Result<TrainPageQueryPersonVO> projectPageQuery(TrainPageQueryDTO trainPageQueryDTO) {
        log.info("查询项目授权清单,{}",trainPageQueryDTO);
        TrainPageQueryPersonVO trainPageQueryPersonVO = trainService.queryProject(trainPageQueryDTO);
        return Result.success(trainPageQueryPersonVO, "根据项目id分页查询项目授权清单成功");
    }

    /**
     * 根据项目id导出项目授权清单
     * @param response
     * @param project_id
     */
    @GetMapping("/export_project/{project_id}")
    public Result<String> export_project(HttpServletResponse response, @PathVariable int project_id) {
        log.info("导出项目授权清单,{}",project_id);
        excelUtil.exportProjectExcel(response, project_id);
        return Result.success("导出项目授权清单成功");
    }
}
